{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "51a0824b-eb68-445a-88c8-8d698cd7b92a",
   "metadata": {},
   "source": [
    "# 一、深入numpy\n",
    "## 1.1、numpy的数据的增删\n",
    "### 1.1.1、数据追加和插入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7dfbcd2f-a1d0-46eb-b985-9de7c977c573",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 4 5]\n",
      "[[1 2]\n",
      " [3 4]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "(3, 2) (2,) (1, 2)\n",
      "[[1 2 5]\n",
      " [3 4 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5])\n",
    "#会产生一个新的数组\n",
    "arr2 = np.append(arr,[4,5])\n",
    "print(arr2)\n",
    "arr1 = np.array([[1,2],[3,4]])\n",
    "print(arr1)\n",
    "arr2 = np.append(arr1,[[5,6]],axis=0)\n",
    "print(arr2)\n",
    "#要在某个方向上拼接（axis=X），拼接数组的维度数必须相同，并且除了 axis 这一维外，其余维度都要对齐。\n",
    "#所以要在行拼接，必须行上要相等\n",
    "print(arr2.shape,np.array([5,6]).shape,np.array([[5,6]]).shape)\n",
    "#如果希望在列上拼接就不能使用[[5,6]]，因为此时是1,2,需要2，1才行[[5],[6]]2\n",
    "arr3 = np.append(arr1,[[5],[6]],axis=1)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8badcf86-1777-41a1-85b8-8224d77a44f9",
   "metadata": {},
   "source": [
    "axis 表示“轴”的方向”，也就是沿着哪一个维度进行操作。\n",
    "简单理解：\n",
    "- axis=0 → 沿着行方向（纵向）操作\n",
    "- axis=1 → 沿着列方向（横向）操作\n",
    "\n",
    "当不指定 axis 参数时，NumPy 会先把数组“拉平”（flatten）成一维，再执行操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7e2081a5-79e8-460f-b9af-1e08fca84a73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2 10  3  4  5]\n",
      "[ 1 12  2  3 22  4  5]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [6 7 8]\n",
      " [4 5 6]]\n",
      "[[99  1  2  3]\n",
      " [22  4  5  6]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1,2,3,4,5])\n",
    "arr2 = np.insert(arr1,2,10) #在第二个位置插入元素10\n",
    "print(arr2)\n",
    "arr2 = np.insert(arr1,[1,3],[12,22])#在两个位置插入不同的数据\n",
    "print(arr2)\n",
    "#二维数组的实例\n",
    "arr1 = np.array([[1,2,3],[4,5,6]])\n",
    "print(arr1)\n",
    "#使用insert可以指定位置插入，只要指定插入方向即可，但是位置要对齐\n",
    "arr2 = np.insert(arr1,1,[6,7,8],axis=0)\n",
    "print(arr2)\n",
    "arr3 = np.insert(arr1,0,[99,22],axis=1)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e29285d7-cd97-408e-af01-6eae273fecda",
   "metadata": {},
   "source": [
    "### 1.1.2、数据的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e9557ab3-b516-4fca-ba2c-d2a57328de54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 5]\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "****************************************************************************************************\n",
      "[[4 5 6]]\n",
      "[[2 3]\n",
      " [5 6]\n",
      " [8 9]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1,2,3,4,5])\n",
    "arr2 = np.delete(arr,[1,3])\n",
    "print(arr2)\n",
    "arr = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#删除0和2两行\n",
    "arr2 = np.delete(arr,[0,2],axis=0)\n",
    "print(arr2)\n",
    "#纵列删除第0纵列\n",
    "arr3 = np.delete(arr,0,axis=1)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b20c79d7-6370-4689-9294-13c973da9ce8",
   "metadata": {},
   "source": [
    "### 1.1.3、数据的合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a38c9f31-ac7d-420b-92f3-53a70c36911c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [11 22 33]]\n",
      "[[11  1  2  3]\n",
      " [12  4  5  6]\n",
      " [13  7  8  9]]\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [11 22 33]]\n",
      "[[ 1  2  3 11]\n",
      " [ 4  5  6 12]\n",
      " [ 7  8  9 13]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(1,10).reshape(3,-1)\n",
    "print(a)\n",
    "b = np.array([[11,22,33]])\n",
    "#numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting=\"same_kind\")\n",
    "#维数必须完全对齐才能拼接\n",
    "arr1 = np.concatenate((a,b),axis=0)\n",
    "print(arr1)\n",
    "#b的维度和a的维度不一样，不能拼接，必须形状和维度都相同才能拼接\n",
    "# arr2 = np.concatenate((a,b),axis=1)\n",
    "c = np.array([[11],[12],[13]])\n",
    "arr2 = np.concatenate((c,a),axis=1)\n",
    "print(arr2)\n",
    "#可以通过vstack(axis=0)和hstack(axis=1)进行垂直拼接和水平拼接\n",
    "arr1 = np.vstack((a,b))\n",
    "print(arr1)\n",
    "arr2 = np.hstack((a,c))\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06b7b034-d47f-4c1a-bc54-dfefd8e80335",
   "metadata": {},
   "source": [
    "### 1.1.4、练习\n",
    "#### **追加和插入**\n",
    " **基础题：**\n",
    "  - 创建数组 `arr = np.array([1,2,3,4,5])`，在末尾追加 `[6,7,8]`。  \n",
    "  - 将上题中的数组改成二维形状 `(2,4)`，在行方向追加一行 `[9,10,11,12]`。  \n",
    "  - 在一维数组 `[10,20,30,40]` 的索引 2 位置插入元素 `99`。  \n",
    "  - 创建二维数组  \n",
    "    ```\n",
    "    [[1,2,3],\n",
    "     [4,5,6],\n",
    "     [7,8,9]]\n",
    "    ```\n",
    "    在第二行（索引 1）后插入新列 `[11,22,33]`。  \n",
    "\n",
    "***\n",
    "\n",
    "**进阶题：**\n",
    "  - 创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，在列方向插入一列全 0。  \n",
    "  - 在行方向追加一行 `[100,200,300,400]`。  \n",
    "  - 先把数组展平，再在索引 5 的位置插入 `999`。  \n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "cb673abc-914d-4001-8f06-d6d396c61a4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8]\n",
      "[[1 2 3 4]\n",
      " [5 6 7 8]]\n",
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "[ 1  2 99  3  4  5  6  7  8]\n",
      "[[ 1  2 11  3]\n",
      " [ 4  5 22  6]\n",
      " [ 7  8 33  9]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#创建数组 arr = np.array([1,2,3,4,5])，在末尾追加 [6,7,8]。\n",
    "arr = np.array([1,2,3,4,5])\n",
    "arr2 = np.append(arr,[6,7,8])\n",
    "print(arr2)\n",
    "#将上题中的数组改成二维形状 (2,4)，在行方向追加一行 [9,10,11,12]。\n",
    "arr3 = arr2.reshape(2,4)\n",
    "print(arr3)\n",
    "arr4 = np.append(arr3,[[9,10,11,12]],axis=0)\n",
    "print(arr4)\n",
    "#在一维数组 arr2 的索引 2 位置插入元素 99。\n",
    "arr5 = np.insert(arr2,2,99)\n",
    "print(arr5)\n",
    "'''\n",
    "创建二维数组\n",
    " [[1,2,3],\n",
    "  [4,5,6],\n",
    "  [7,8,9]]\n",
    "在第二行（索引 1）后插入新列 [11,22,33]。\n",
    "'''\n",
    "arr1 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "arr2 = np.insert(arr1,2,[11,22,33],axis=1)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "edee608a-d1ef-45cb-8484-7b129c4e2984",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "[[ 0  1  2  3  4]\n",
      " [ 0  5  6  7  8]\n",
      " [ 0  9 10 11 12]]\n",
      "[[  1   2   3   4]\n",
      " [  5   6   7   8]\n",
      " [  9  10  11  12]\n",
      " [100 200 300 400]]\n",
      "[  1   2   3   4   5 999   6   7   8   9  10  11  12]\n"
     ]
    }
   ],
   "source": [
    "# 创建二维数组 arr = np.arange(1,13).reshape(3,4)，在列方向插入一列全 0。\n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "print(arr)\n",
    "arr2 = np.insert(arr,0,np.zeros(3),axis=1)\n",
    "print(arr2)\n",
    "# 在行方向追加一行 [100,200,300,400]。\n",
    "arr3 = np.append(arr,[[100,200,300,400]],axis=0)\n",
    "print(arr3)\n",
    "# 先把数组展平，再在索引 5 的位置插入 999。\n",
    "arr4 = np.insert(arr,5,999)\n",
    "print(arr4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c517310-3e6a-449a-9417-a140b604f320",
   "metadata": {},
   "source": [
    "#### **数据删除**\n",
    "**基础题：**\n",
    "  - 创建数组 `[10,20,30,40,50]`，删除索引 2 的元素。  \n",
    "  - 创建二维数组  \n",
    "    ```\n",
    "    [[1,2,3],\n",
    "     [4,5,6],\n",
    "     [7,8,9]]\n",
    "    ```\n",
    "    删除第 1 行（索引 0）。  \n",
    "  - 删除上面数组的第 2 列（索引 1）。  \n",
    "  - 删除一维数组 `[1,2,3,4,5,6,7,8]` 中所有偶数（提示：用布尔条件取索引）。  \n",
    "\n",
    "***\n",
    "\n",
    "**进阶题：**\n",
    "  - 创建 `arr = np.arange(1,13).reshape(3,4)`，删除所有大于 8 的元素。  \n",
    "  - 删除第一行和最后一列。  \n",
    "  - 删除第 1、3、5 索引位置的元素（可使用列表）。  \n",
    "  - 删除二维数组的所有奇数列。  \n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9470e1b0-702c-42ba-a315-7a04e31c1d31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 40 50]\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "****************************************************************************************************\n",
      "[[4 5 6]\n",
      " [7 8 9]]\n",
      "[[1 3]\n",
      " [4 6]\n",
      " [7 9]]\n",
      "[1 2 3 4 5 6 7 8]\n",
      "(array([1, 3, 5, 7]),)\n",
      "[1 3 5 7]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#创建数组 [10,20,30,40,50]，删除索引 2 的元素。\n",
    "arr = np.array([10,20,30,40,50])\n",
    "arr2 = np.delete(arr,2)\n",
    "print(arr2)\n",
    "'''创建二维数组\n",
    "[[1,2,3],\n",
    " [4,5,6],\n",
    " [7,8,9]]\n",
    "删除第 1 行（索引 0）。\n",
    "删除上面数组的第 2 列（索引 1）。\n",
    "'''\n",
    "arr = np.arange(1,10).reshape(3,3)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "arr2 = np.delete(arr,0,axis=0)\n",
    "arr3 = np.delete(arr,1,axis=1)\n",
    "print(arr2)\n",
    "print(arr3)\n",
    "#删除一维数组 [1,2,3,4,5,6,7,8] 中所有偶数（提示：用布尔条件取索引）。\n",
    "arr = np.arange(1,9)\n",
    "print(arr)\n",
    "print(np.where(arr%2==0))\n",
    "arr2 = np.delete(arr,np.where(arr%2==0))\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "573e9674-c370-43e5-a53d-4ba2dec48ff4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "****************************************************************************************************\n",
      "[1 2 3 4 5 6 7 8]\n",
      "[[ 5  6  7]\n",
      " [ 9 10 11]]\n",
      "[1 3 5 7 8 9]\n",
      "[[ 1  3]\n",
      " [ 5  7]\n",
      " [ 9 11]]\n"
     ]
    }
   ],
   "source": [
    "#创建 arr = np.arange(1,13).reshape(3,4)，删除所有大于 8 的元素。\n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "arr2 = arr[arr<=8]\n",
    "print(arr2)\n",
    "#删除第一行和最后一列。\n",
    "arr2 = np.delete(arr,0,axis=0)\n",
    "arr3 = np.delete(arr2,-1,axis=1)\n",
    "print(arr3)\n",
    "#删除第 1、3、5 索引位置的元素（可使用列表）。\n",
    "arr = np.array([1,2,3,4,5,6,7,8,9])\n",
    "arr2 = np.delete(arr,[1,3,5])\n",
    "print(arr2)\n",
    "#删除二维数组的所有奇数列。\n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "idx = np.arange(arr.shape[1]) \n",
    "ts = idx[idx%2!=0]\n",
    "arr2 = np.delete(arr,ts,axis=1)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c0d5a5e-2c07-4f4f-85c7-2deff4382b28",
   "metadata": {},
   "source": [
    "#### **数据合并**\n",
    "**基础题：**\n",
    "  - 创建两个数组  \n",
    "    ```\n",
    "    a = [[1,2],\n",
    "         [3,4]]\n",
    "    b = [[5,6],\n",
    "         [7,8]]\n",
    "    ```\n",
    "    分别沿 axis=0 和 axis=1 拼接。  \n",
    "  - 将三个一维数组 `[1,2]`、`[3,4]`、`[5,6]` 合并成一个。  \n",
    "  - 将两个 `(2,3)` 数组纵向拼接成 `(4,3)`。  \n",
    "  - 将两个 `(3,2)` 数组横向拼接成 `(3,4)`。  \n",
    "\n",
    "***\n",
    "\n",
    "**进阶题：**\n",
    "  - 创建 `a = np.arange(6).reshape(2,3)`，`b = np.arange(6,12).reshape(2,3)`，按行拼接。  \n",
    "  - 创建 `a = np.arange(9).reshape(3,3)`，将它与自身横向拼接（重复一次）。  \n",
    "  - 尝试将形状不同的数组合并，观察报错信息并解释原因。  \n",
    "  - 将 `[1,2,3]`、`[4,5,6]`、`[7,8,9]` 这三个一维数组合并为一个 3×3 二维数组。  \n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0fd1571e-f039-4dca-ae3c-ba802dc0fcda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n",
      "[1 2 3 4 5 6]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[5 4 6]\n",
      " [3 1 3]]\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [5 4 6]\n",
      " [3 1 3]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[8 3]\n",
      " [4 5]\n",
      " [8 6]]\n",
      "[[1 2 8 3]\n",
      " [3 4 4 5]\n",
      " [5 6 8 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "arr1 = np.concatenate((a,b),axis=0)\n",
    "arr2 = np.concatenate((a,b),axis=1)\n",
    "print(arr1)\n",
    "print(arr2)\n",
    "#将三个一维数组 [1,2]、[3,4]、[5,6] 合并成一个。\n",
    "a = np.array([1,2])\n",
    "b = np.array([3,4])\n",
    "c = np.array([5,6])\n",
    "arr = np.concatenate((a,b,c),axis=0)#原来是一维的，拼接之后还是一维的\n",
    "arr1 = np.concatenate(([a],[b],[c]),axis=0) #拼接之后就是二维的数组\n",
    "print(arr)\n",
    "print(arr1)\n",
    "#将两个 (2,3) 数组纵向拼接成 (4,3)。\n",
    "a = np.arange(1,7).reshape(2,3)\n",
    "b = np.random.randint(1,10,(2,3))\n",
    "print(a)\n",
    "print(b)\n",
    "arr1 = np.concatenate((a,b),axis=0)\n",
    "print(arr1)\n",
    "#将两个 (3,2) 数组横向拼接成 (3,4)。\n",
    "a = np.arange(1,7).reshape(3,2)\n",
    "b = np.random.randint(1,10,(3,2))\n",
    "print(a)\n",
    "print(b)\n",
    "arr1 = np.concatenate((a,b),axis=1)\n",
    "print(arr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cab56ba-1b95-41c1-9cf7-d8c69f5158f0",
   "metadata": {},
   "source": [
    "## 1.2、numpy的数据修改\n",
    "### 1.2.1、直接和基于切片修改元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b8e559ae-8d48-4522-9357-c814b5424bf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12  2  3  4  5]\n",
      "[12  4  2  1  5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "arr[0] = 12 #直接修补\n",
    "print(arr)\n",
    "arr[1:4] = [4,2,1]\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ed9f8430-66a1-4e10-a6af-3abfb2dd051d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "****************************************************************************************************\n",
      "[[ 1  2  3]\n",
      " [ 4  5 12]\n",
      " [ 7  8  9]]\n",
      "[[1 2 3]\n",
      " [2 3 2]\n",
      " [7 8 9]]\n",
      "[[1 3 3]\n",
      " [2 3 2]\n",
      " [7 3 9]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(arr2)\n",
    "print(\"*\"*100)\n",
    "arr2[1,2] = 12\n",
    "print(arr2)\n",
    "arr2[1] = [2,3,2]#修改第一行\n",
    "print(arr2)\n",
    "arr2[:,1] = [3,3,3] #把第一列修改为3,3,3\n",
    "print(arr2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "65e4f03a-f612-465a-959b-f0d25e538108",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11 12 13 14 15]\n",
      "[110  12 130  14 150]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1,2,3,4,5])\n",
    "arr+=10\n",
    "print(arr)\n",
    "arr[::2]*=10\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b7a11fc-2471-44c3-8eb5-972cae045ff9",
   "metadata": {},
   "source": [
    "### 1.2.2、基于花式索引的修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "27e0c74c-6e86-4ff0-94a4-8b494854a375",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 30 40 50 60]\n",
      "[ 10 111  30 222  50 333]\n",
      "[[  1   2   3]\n",
      " [  4   5 888]\n",
      " [  7 999   9]]\n",
      "[[  1  99  99]\n",
      " [  4   5 888]\n",
      " [  7  99  99]]\n",
      "[[  1  99  99]\n",
      " [  4   5 888]\n",
      " [  7  99  99]]\n",
      "[[149  67 151 122  65 109 113  58 146  84  93  63 138 156 115]\n",
      " [116  71 154 144 132 104  66  86  94 112 117 147 138 156 137]\n",
      " [ 55  53  65 124 138  92  69 112 113  54 109 132  96  87  71]\n",
      " [ 85 121  81  72 141 126  51  78 149  95  73  63 120  53 122]\n",
      " [124 137 111 128 122  63 130  87 105 103 110 116 140  53 140]\n",
      " [ 83 151 123 146  54  68  75  94 133 157 130 133  89 123  82]\n",
      " [114 144  82 155 132  90  93  93 132 125 115  69  60  70  74]\n",
      " [ 89  90 156  60  96 105  78 127 156 131 138 119  66 126 159]\n",
      " [ 81  70  79 127 150  54  90 153  71 151  96 130  69  99  99]\n",
      " [154  64  60 126 108  92 133  61  55 139 104 118 128 125  59]]\n",
      "****************************************************************************************************\n",
      "[[149  67   0 122  65 109 113  58 146  84  93  63 138   0 115]\n",
      " [116  71   0 144 132 104  66  86  94 112 117 147 138   0 137]\n",
      " [ 55  53  65 124 138  92  69 112 113  54 109 132  96  87  71]\n",
      " [ 85 121  81  72 141 126  51  78 149  95  73  63 120  53 122]\n",
      " [124 137 111 128 122  63 130  87 105 103 110 116 140  53 140]\n",
      " [ 83   0 123 146  54  68  75  94 133   0 130 133  89 123  82]\n",
      " [114 144  82   0 132  90  93  93 132 125 115  69  60  70  74]\n",
      " [ 89  90   0  60  96 105  78 127   0 131 138 119  66 126   0]\n",
      " [ 81  70  79 127 150  54  90   0  71   0  96 130  69  99  99]\n",
      " [  0  64  60 126 108  92 133  61  55 139 104 118 128 125  59]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([10, 20, 30, 40, 50, 60])\n",
    "print(arr)\n",
    "arr[[1,3,5]] = [111,222,333]\n",
    "print(arr)\n",
    "arr2 = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 9]\n",
    "])\n",
    "#修改(1,2)和(2,1)两个位置的元素\n",
    "arr2[[1,2],[2,1]] = [888,999]\n",
    "print(arr2)\n",
    "#把0-2两行中的1->2的元素改成99\n",
    "arr2[[0,2],1:3] = 99\n",
    "print(arr2)\n",
    "#0和2两行中的0和1两列的元素修改为-10\n",
    "arr2[np.ix_([0,2],[0,1])]-=0\n",
    "print(arr2)\n",
    "arr = np.random.randint(50,160,(10,15))\n",
    "print(arr)\n",
    "#花式索引的修改\n",
    "arr[arr>150] = 0\n",
    "print(\"*\"*100)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ff5f15a-687a-40ca-a4fa-f6ad00627b0c",
   "metadata": {},
   "source": [
    "### 1.2.3、常用的修改方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0db7431a-cdeb-403e-baf9-cc065cf77e13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1 88  3  4 99  6  7]\n",
      "[[1 2 3 4]\n",
      " [5 6 7 8]]\n",
      "****************************************************************************************************\n",
      "[[999 777   3   4]\n",
      " [  5   6 666   8]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#np.put修改一维数组\n",
    "arr = np.array([1,2,3,4,5,6,7])\n",
    "np.put(arr,[1,4],[88,99])\n",
    "print(arr)\n",
    "#二维数组会自动转换为一维来处理\n",
    "arr = np.array([[1,2,3,4],[5,6,7,8]])\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#修改之后会自动将为一维\n",
    "np.put(arr,[0,1,6],[999,777,666])\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9502c694-5774-424b-bd4e-62184f69d8b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[59 29 91 71 69 56 73 17 24 96]\n",
      " [42 87 71 92 39 62 79 41 34 38]\n",
      " [33 27 12 86 12 77 39 14 62 31]\n",
      " [16 17 94 70 73 58 77 27 71 27]\n",
      " [72 40 24 16 65 23 40 77 45 63]\n",
      " [63 91 16 39 34 87 35 98 94 49]\n",
      " [80 17 73 11 71 52 66 74 69 53]\n",
      " [36 21 76 22 46 26 13 74 99 46]\n",
      " [16 97 83 10 96 93 68 38 69 42]\n",
      " [19 38 39 77 52 14 90 15 56 25]]\n",
      "****************************************************************************************************\n",
      "[[59 29 91 71 69 56 73  0 24 96]\n",
      " [42 87 71 92 39 62 79 41 34 38]\n",
      " [33 27  0 86  0 77 39  0 62 31]\n",
      " [ 0  0 94 70 73 58 77 27 71 27]\n",
      " [72 40 24  0 65 23 40 77 45 63]\n",
      " [63 91  0 39 34 87 35 98 94 49]\n",
      " [80  0 73  0 71 52 66 74 69 53]\n",
      " [36 21 76 22 46 26  0 74 99 46]\n",
      " [ 0 97 83  0 96 93 68 38 69 42]\n",
      " [ 0 38 39 77 52  0 90  0 56 25]]\n",
      "[[159 129 191 171 169 156 173   0 124 196]\n",
      " [142 187 171 192 139 162 179 141 134 138]\n",
      " [133 127   0 186   0 177 139   0 162 131]\n",
      " [  0   0 194 170 173 158 177 127 171 127]\n",
      " [172 140 124   0 165 123 140 177 145 163]\n",
      " [163 191   0 139 134 187 135 198 194 149]\n",
      " [180   0 173   0 171 152 166 174 169 153]\n",
      " [136 121 176 122 146 126   0 174 199 146]\n",
      " [  0 197 183   0 196 193 168 138 169 142]\n",
      " [  0 138 139 177 152   0 190   0 156 125]]\n"
     ]
    }
   ],
   "source": [
    "#基于where的修改,非常常用np.where(条件,满足的时候，不满足的时候)\n",
    "arr = np.random.randint(10,100,(10,10))\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#小于20的设置0，否则不变\n",
    "arr1 = np.where(arr<20,0,arr)\n",
    "print(arr1)\n",
    "arr2 = np.where(arr<20,0,arr+100)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a19691da-f569-4653-956c-28b812c6dc62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[99  2  3]\n",
      " [ 4 99  6]\n",
      " [ 7  8 99]]\n"
     ]
    }
   ],
   "source": [
    "#np.put 的作用：按“扁平化索引”把值写回数组（就地修改）。它先把数组当作一维 ravel() 视图，然后在这些位置写入给定的值。\n",
    "import numpy as np\n",
    "\n",
    "arr = np.arange(1, 10).reshape(3, 3)\n",
    "# arr:\n",
    "# [[1, 2, 3],\n",
    "#  [4, 5, 6],\n",
    "#  [7, 8, 9]]\n",
    "\n",
    "np.put(arr, [0, 4, 8], 99)   # 修改扁平索引 0、4、8（主对角线）\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b62a0b8-d427-4f9b-9655-1768a0a6b28f",
   "metadata": {},
   "source": [
    "# 二、综合练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02e83e54-2a9e-4914-a47f-a947813368db",
   "metadata": {},
   "source": [
    "### 1.1、NumPy 综合练习题（数据的增删改查综合·无答案·Markdown 源码）\n",
    "\n",
    "> 适用范围：索引 / 切片 / 添加 / 删除 / 拼接 / 修改 / 花式索引 / 布尔索引 / where / put  \n",
    "> 使用前请导入：`import numpy as np`\n",
    "#### 练习1\n",
    "***\n",
    "- 1、创建一个长度为10的一维全为0的ndarray对象，然后让第5个元素等于1\n",
    "  \n",
    "- 2、创建一个元素为从10到49的ndarray对象，间隔是1\n",
    "\n",
    "- 3、将第2题的所有元素位置反转\n",
    "\n",
    "- 4、使用np.random.random创建一个10\\*10的ndarray对象，并打印出最大最小元素(arr.max(),arr.min()函数)\n",
    "\n",
    "- 5、创建一个10\\*10的ndarray对象，且矩阵边界全为1，里面全为0\n",
    "\n",
    "- 6、创建一个每一行都是从0到4的5\\*5矩阵\n",
    "\n",
    "- 7、创建一个范围在(0,1)之间的长度为12的等差数列。\n",
    "\n",
    "- 8、创建一个长度为10的正太分布数组np.random.randn并排序（np.sort()）\n",
    "\n",
    "- 9、创建一个长度为10的随机数组并将最大值替换为-100\n",
    "\n",
    "- 10、如何根据第3列大小顺序来对一个5\\*5矩阵排序？\n",
    "\n",
    "#### 练习2\n",
    "***\n",
    "\n",
    "- （1）创建数组 `arr = np.arange(10, 20)`，取出第 3~7 个元素；再取出所有奇数索引位置的元素；并将最后三个元素改为 `0`。  \n",
    "\n",
    "- （2）创建二维数组 `arr2 = np.arange(1,13).reshape(3,4)`，取出第二行和第三列；将第二行的所有元素改为 `99`；再将所有大于 `8` 的元素改为 `0`。  \n",
    "\n",
    "- （3）创建数组 `arr = np.array([1,2,3,4,5])`，在末尾追加 `[6,7,8]`；然后在索引 `2` 位置插入 `99`；在索引 `3` 位置插入 `[100,101]`。  \n",
    "\n",
    "- （4）创建二维数组 `arr = np.arange(1,10).reshape(3,3)`，在第 `1` 列插入新列 `[100,200,300]`（`axis=1`）。  \n",
    "\n",
    "- （5）创建数组 `arr = np.arange(1,11)`，删除索引 `[1,3,5]` 的元素；再删除当前数组中的所有偶数元素（使用布尔索引）。  \n",
    "\n",
    "- （6）创建二维数组 `arr = np.arange(12).reshape(3,4)`，删除第 `0` 行与第 `2` 列（分别指定 `axis=0/1`）。  \n",
    "\n",
    "- （7）给定三个数组  \n",
    "    a = `np.array([1,2])`，b = `np.array([3,4])`，c = `np.array([5,6])`：  \n",
    "    ① 使用 `np.concatenate` 合并为一维；  \n",
    "    ② 使用 `np.stack` 合并为二维（3×2）；  \n",
    "    ③ 分别用 `np.vstack` 与 `np.hstack` 展示纵向与横向拼接。  \n",
    "\n",
    "- （8）创建数组 `arr = np.array([10,20,30,40,50,60])`，用花式索引将索引 `[1,3,5]` 的元素同时改为 `-1`。  \n",
    "\n",
    "- （9）创建数组 `arr = np.array([10,20,30,40,50])`，将所有 `≥ 40` 的元素改为 `0`。  \n",
    "\n",
    "- （10）创建数组 `arr = np.arange(1,11)`，使用 `np.where` 将偶数改为 `0`，奇数保持原值。  \n",
    "\n",
    "- （11）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，使用 `np.put` 将线性位置 `0、5、10` 的元素改为 `99`（按一维顺序）。  \n",
    "\n",
    "- （12）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，将第一行所有元素加 `10`；再将最后一列全部改为 `0`。  \n",
    "\n",
    "- （13）创建数组 `arr = np.array([5,10,15,20,25,30])`，将 `> 20` 的元素改为 `-1`；将等于 `10` 的元素改为 `0`。  \n",
    "\n",
    "- （14）创建二维数组  \n",
    "      arr =  \n",
    "          [ [ 5,  2,  8,  1],  \n",
    "            [ 7,  9,  3,  4],  \n",
    "            [ 6,  0, 11, 12] ]  \n",
    "    依次完成：删除第 `2` 行；在末尾添加一行 `[100,101,102,103]`；将第 `0` 行的偶数改为 `0`；将第 `1` 列复制到末列（原列保留）；使用 `np.where` 将所有 `> 50` 的值改为 `-1`。  \n",
    "\n",
    "- （15）创建数组 `arr = np.arange(1,21).reshape(4,5)`，依次完成：删除第 `1` 行；在第 `2` 列插入 `[99,99,99]`；将所有 `> 10` 的元素改为 `0`；将第 `0` 行的奇数改为 `-1`；使用 `np.put` 将线性位置 `0、5、10` 的值设为 `888`；输出最终结果。  \n",
    "\n",
    "***\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f1cb861-4f85-423d-a7b6-15c60b171942",
   "metadata": {},
   "source": [
    "## 1.2、综合练习答案\n",
    "### 1.2.1、练习一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "993270fb-a6d0-48fd-ac28-abb3c09c6b8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]\n",
      "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n",
      " 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n",
      "[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26\n",
      " 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10]\n",
      "[[0.78120943 0.5261415  0.45082664 0.88033673 0.07079616 0.04266169\n",
      "  0.98703295 0.40946599 0.43076289 0.8630535 ]\n",
      " [0.94197357 0.42296058 0.12998329 0.36526768 0.22499786 0.32470627\n",
      "  0.75794901 0.21988455 0.57727036 0.59952276]\n",
      " [0.74484504 0.32994409 0.89324563 0.61132861 0.7561378  0.93340434\n",
      "  0.594786   0.56399476 0.49344067 0.54789776]\n",
      " [0.72491612 0.99720741 0.991884   0.79236527 0.81028249 0.94788976\n",
      "  0.70098945 0.27332456 0.13642276 0.84890751]\n",
      " [0.74933069 0.93513617 0.34817344 0.19696167 0.21434104 0.30463004\n",
      "  0.51835529 0.26365128 0.51666651 0.70650803]\n",
      " [0.564474   0.3030147  0.6512732  0.55132948 0.83199907 0.95221576\n",
      "  0.83029488 0.53360053 0.85443273 0.87996274]\n",
      " [0.94089157 0.55916379 0.03964056 0.08011164 0.07581737 0.08084921\n",
      "  0.47308559 0.65010454 0.89637672 0.7385293 ]\n",
      " [0.47503316 0.23746231 0.71496054 0.78337233 0.34768514 0.19574662\n",
      "  0.98278961 0.26467424 0.6081429  0.13640975]\n",
      " [0.1031647  0.58221764 0.10317341 0.70314768 0.40107527 0.82526335\n",
      "  0.91610971 0.35073612 0.88216594 0.92354007]\n",
      " [0.01290722 0.24106352 0.06008632 0.13845888 0.95717337 0.16082933\n",
      "  0.09962722 0.24447213 0.17794719 0.46081162]]\n",
      "0.012907220623513349 0.9972074070229796\n",
      "[[1 1 1 1 1 1 1 1 1 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 0 0 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]]\n",
      "[[0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]]\n",
      "[1.000e+00 2.000e+00 4.000e+00 8.000e+00 1.600e+01 3.200e+01 6.400e+01\n",
      " 1.280e+02 2.560e+02 5.120e+02 1.024e+03]\n",
      "[-0.56446621 -0.33186071 -1.3590423   0.7162101  -1.47749901  0.18754633\n",
      " -1.5001852   1.17100523 -0.51679272  0.07932995]\n",
      "[-1.5001852  -1.47749901 -1.3590423  -0.56446621 -0.51679272 -0.33186071\n",
      "  0.07932995  0.18754633  0.7162101   1.17100523]\n",
      "[[ 5 10 16  6  6]\n",
      " [19 17 13  3 18]\n",
      " [18 15 19 13  7]\n",
      " [14  2 13 13 13]\n",
      " [10 11  1 10 18]]\n",
      "****************************************************************************************************\n",
      "19\n",
      "[[  5  10  16   6   6]\n",
      " [100  17  13   3  18]\n",
      " [ 18  15 100  13   7]\n",
      " [ 14   2  13  13  13]\n",
      " [ 10  11   1  10  18]]\n",
      "[[4 3 9 2 4]\n",
      " [2 4 7 4 8]\n",
      " [4 8 4 1 2]\n",
      " [2 9 5 7 2]\n",
      " [7 6 1 8 5]]\n",
      "****************************************************************************************************\n",
      "[3 4 2 0 1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#1、创建一个长度为10的一维全为0的ndarray对象，然后让第5个元素等于1**\n",
    "arr = np.zeros(10)\n",
    "arr[5] = 1\n",
    "print(arr)\n",
    "#2、创建一个元素为从10到49的ndarray对象\n",
    "arr = np.arange(10,50)\n",
    "print(arr)\n",
    "#3、将第2题的所有元素位置反转\n",
    "arr1 = arr[::-1]\n",
    "print(arr1)\n",
    "#4、使用np.random.random创建一个10*10的ndarray对象，并打印出最大最小元素\n",
    "arr = np.random.rand(10,10)\n",
    "print(arr)\n",
    "print(arr.min(),arr.max())\n",
    "#5、创建一个10*10的ndarray对象，且矩阵边界全为1，里面全为0\n",
    "arr = np.zeros((10,10),dtype=np.int16)\n",
    "arr[[0,-1]] = 1\n",
    "arr[:,[0,-1]] = 1\n",
    "print(arr)\n",
    "#6、创建一个每一行都是从0到4的5*5矩阵\n",
    "arr = np.zeros((5,5))\n",
    "arr[0:5] = np.arange(0,5)\n",
    "print(arr)\n",
    "#7、创建一个范围在(0,1)之间的长度为12的等差数列。\n",
    "arr1 = np.linspace(0,1,12)\n",
    "print(arr2)\n",
    "#8、创建一个长度为10的正太分布数组np.random.randn并排序\n",
    "arr = np.random.randn(10)\n",
    "print(arr)\n",
    "arr1 = np.sort(arr)\n",
    "print(arr1)\n",
    "#9、创建一个长度为5*5的随机数组并将最大值替换为-100\n",
    "arr = np.random.randint(1,20,(5,5))\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "print(arr.max())\n",
    "arr[arr==arr.max()] = 100\n",
    "print(arr)\n",
    "#10、如何根据第3列大小顺序来对一个5*5矩阵排序？（考察argsort()方法）\n",
    "arr = np.random.randint(1,10,(5,5))\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "idx = np.argsort(arr[2])\n",
    "print(idx)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ffca6da-2eb1-425e-8f39-9df2687b2652",
   "metadata": {},
   "source": [
    "### 1.2.2、练习二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ca2fafbd-f41f-417b-baa7-a437276e3adb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[13 14 15 16]\n",
      "[ 10   0  12   0  14   0  16   0  18   0  20   0  22   0  24   0  26   0\n",
      "  28   0  30   0  32   0  34   0  36   0  38   0  40   0  42   0  44   0\n",
      "  46   0  48   0  50   0  52   0  54   0  56   0  58   0  60   0  62   0\n",
      "  64   0  66   0  68   0  70   0  72   0  74   0  76   0  78   0  80   0\n",
      "  82   0  84   0  86   0  88   0  90   0  92   0  94   0  96   0  98   0\n",
      " 100   0 102   0 104   0 106   0 108   0 110   0 112   0 114   0 116   0\n",
      " 118   0]\n",
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "****************************************************************************************************\n",
      "[5 6 7 8]\n",
      "[[ 3]\n",
      " [ 7]\n",
      " [11]]\n",
      "[[1 2 3 4]\n",
      " [0 0 0 0]\n",
      " [0 0 0 0]]\n",
      "[1 2 3 4 5 6 7 8]\n",
      "[ 1  2 99  3  4  5  6  7  8]\n",
      "[  1   2  99 100 101   3   4   5   6   7   8]\n",
      "[[  1 100   2   3]\n",
      " [  4 200   5   6]\n",
      " [  7 300   8   9]]\n",
      "[ 1  2  3  4  5  6  7  8  9 10]\n",
      "[ 1  3  5  7  8  9 10]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[[ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[[ 0  1  3]\n",
      " [ 4  5  7]\n",
      " [ 8  9 11]]\n",
      "[1 2 3 4 5 6]\n",
      "****************************************************************************************************\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "****************************************************************************************************\n",
      "[[1]\n",
      " [2]]\n",
      "[[1 3 5]\n",
      " [2 4 6]]\n",
      "****************************************************************************************************\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 3 5]\n",
      " [2 4 6]]\n",
      "****************************************************************************************************\n",
      "[20 40 60]\n",
      "****************************************************************************************************\n",
      "[10 20 30  0  0]\n",
      "[1 0 3 0 5 0 7 0 9 0]\n",
      "[[99  2  3  4]\n",
      " [ 5 99  7  8]\n",
      " [ 9 10 99 12]]\n",
      "[[11 12 13 14]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "[[11 12 13  0]\n",
      " [ 5  6  7  0]\n",
      " [ 9 10 11  0]]\n",
      "[ 5  0 15 20 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#（1）创建数组 `arr = np.arange(10, 20)`，取出第 3~7 个元素；再取出所有奇数索引位置的元素；并将最后三个元素改为 `0`。  \n",
    "arr = np.arange(10,120)\n",
    "print(arr[3:7])\n",
    "arr[1::2] = 0\n",
    "print(arr)\n",
    "#（2）创建二维数组 `arr2 = np.arange(1,13).reshape(3,4)`，取出第二行和第三列；将第二行的所有元素改为 `99`；再将所有大于 `8` 的元素改为 `0`。  \n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "print(arr[1])\n",
    "print(arr[:,2:3])\n",
    "arr[1] = 99\n",
    "arr[arr>8] = 0\n",
    "print(arr)\n",
    "#（3）创建数组 `arr = np.array([1,2,3,4,5])`，在末尾追加 `[6,7,8]`；然后在索引 `2` 位置插入 `99`；在索引 `3` 位置插入 `[100,101]`。\n",
    "arr = np.array([1,2,3,4,5])\n",
    "arr2 = np.append(arr,[6,7,8])\n",
    "print(arr2)\n",
    "arr3 = np.insert(arr2,2,99)\n",
    "print(arr3)\n",
    "arr4 = np.insert(arr3,3,[100,101])\n",
    "print(arr4)\n",
    "#（4）创建二维数组 `arr = np.arange(1,10).reshape(3,3)`，在第 `1` 列插入新列 `[100,200,300]`（`axis=1`）。  \n",
    "arr = np.arange(1,10).reshape(3,3)\n",
    "arr2 = np.insert(arr,1,[100,200,300],axis=1)\n",
    "print(arr2)\n",
    "#（5）创建数组 `arr = np.arange(1,11)`，删除索引 `[1,3,5]` 的元素；再删除当前数组中的所有偶数元素（使用布尔索引）。  \n",
    "arr = np.arange(1,11)\n",
    "arr2 = np.delete(arr,[1,3,5])\n",
    "print(arr)\n",
    "print(arr2)\n",
    "#（6）创建二维数组 `arr = np.arange(12).reshape(3,4)`，删除第 `0` 行与第 `2` 列（分别指定 `axis=0/1`）。  \n",
    "arr = np.arange(12).reshape(3,4)\n",
    "print(arr)\n",
    "arr2 = np.delete(arr,0,axis=0)\n",
    "arr3 = np.delete(arr,2,axis=1)\n",
    "print(arr2)\n",
    "print(arr3)\n",
    "#（7）给定三个数组  \n",
    "#   a = `np.array([1,2])`，b = `np.array([3,4])`，c = `np.array([5,6])`：  \n",
    "#    ① 使用 `np.concatenate` 合并为一维；  \n",
    "#    ② 使用 `np.concatenate` 合并为二维（3×2）和(3X2)；  \n",
    "#    ③ 分别用 `np.vstack` 与 `np.hstack` 展示纵向与横向拼接。  \n",
    "a = np.array([1,2])\n",
    "b = np.array([3,4])\n",
    "c = np.array([5,6])\n",
    "arr = np.concatenate((a,b,c))\n",
    "print(arr)\n",
    "arr2 = np.concatenate(([a],[b],[c]),axis=0)\n",
    "print('*'*100)\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "print(a[:,None])\n",
    "arr3 = np.concatenate((a.reshape(-1,1),b.reshape(-1,1),c.reshape(-1,1)),axis=1)\n",
    "print(arr3)\n",
    "print('*'*100)\n",
    "arr2 = np.vstack((a,b,c))\n",
    "print(arr2)\n",
    "print(np.hstack((a.reshape(-1,1),b.reshape(-1,1),c.reshape(-1,1))))\n",
    "#（8）创建数组 `arr = np.array([10,20,30,40,50,60])`，用花式索引将索引 `[1,3,5]` 的元素同时改为 `-1`。  \n",
    "arr = np.array([10,20,30,40,50,60])\n",
    "print('*'*100)\n",
    "print(arr[[1,3,5]])\n",
    "#（9）创建数组 `arr = np.array([10,20,30,40,50])`，将所有 `≥ 40` 的元素改为 `0`。  \n",
    "print('*'*100)\n",
    "arr = np.array([10,20,30,40,50])\n",
    "arr[arr>=40] = 0\n",
    "print(arr)\n",
    "#（10）创建数组 `arr = np.arange(1,11)`，使用 `np.where` 将偶数改为 `0`，奇数保持原值。  \n",
    "arr = np.arange(1,11)\n",
    "arr2 = np.where(arr%2==0,0,arr)\n",
    "print(arr2)\n",
    "#（11）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，使用 `np.put` 将线性位置 `0、5、10` 的元素改为 `99`（按一维顺序）。  \n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "np.put(arr,[0,5,10],99)\n",
    "print(arr)\n",
    "#（12）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，将第一行所有元素加 `10`；再将最后一列全部改为 `0`。  \n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "arr[0]+=10\n",
    "print(arr)\n",
    "arr[:,-1] = 0\n",
    "print(arr)\n",
    "#（13）创建数组 `arr = np.array([5,10,15,20,25,30])`，将 `> 20` 的元素改为 `-1`；将等于 `10` 的元素改为 `0`。  \n",
    "arr = np.array([5,10,15,20,25,30])\n",
    "arr[arr>20] = -1\n",
    "arr[arr==10] = 0\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "12d4262b-d06b-4a98-96ac-bc19ad0c742c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.23579754 0.19683141 0.03897375 0.96248153 0.8128403  0.01503389\n",
      "  0.76374602 0.15905464 0.96908345 0.58092884]\n",
      " [0.74519322 0.80699646 0.63508615 0.85727462 0.99966738 0.45739668\n",
      "  0.22569492 0.26526101 0.10006756 0.78696426]\n",
      " [0.44276421 0.88495193 0.00408326 0.38550905 0.27555867 0.81325256\n",
      "  0.89430801 0.36030995 0.22421489 0.92030977]\n",
      " [0.40016347 0.26892097 0.3509949  0.03203179 0.08490126 0.88726705\n",
      "  0.66672447 0.25438034 0.76014057 0.20338649]\n",
      " [0.253344   0.77157552 0.70521051 0.89505285 0.41651845 0.16390184\n",
      "  0.30548302 0.7733533  0.42341664 0.61099995]\n",
      " [0.55191561 0.16007885 0.54465222 0.86120424 0.77118291 0.23572119\n",
      "  0.63784546 0.01490529 0.35141129 0.58804397]\n",
      " [0.22772387 0.38366705 0.9553088  0.58200116 0.17462241 0.89912515\n",
      "  0.43524235 0.88019476 0.81835704 0.7576853 ]\n",
      " [0.15250484 0.68539996 0.57439908 0.52856106 0.11006706 0.28190094\n",
      "  0.10584432 0.73204288 0.49146091 0.71754922]\n",
      " [0.57206541 0.81628132 0.97949884 0.25712353 0.5148986  0.42356762\n",
      "  0.54151437 0.39453193 0.07436797 0.92462954]\n",
      " [0.52590415 0.15655148 0.98440684 0.94451028 0.17783078 0.52900661\n",
      "  0.35692643 0.17636643 0.93135585 0.47015015]] 0.9996673757725526 0.00408326116829183\n",
      "[0.74519322 0.88495193 0.98440684 0.96248153 0.99966738 0.89912515\n",
      " 0.89430801 0.88019476 0.96908345 0.92462954]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.random((10,10))\n",
    "print(arr,arr.max(),arr.min())\n",
    "print(arr.max(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5845975-5fba-4672-9645-2c1dabee442e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
